package com.dragon.controller;

import com.dragon.pojo.Result;
import com.dragon.pojo.contest.ContestInfo;
import com.dragon.pojo.contest.ContestUserListInfo;
import com.dragon.service.ContestService;
import com.dragon.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("contest")
public class ContestController {
    @Autowired
    ContestService contestService;

    /**
     * 查询比赛列表
     * @param pageNumber 页数
     * @param listNumber 每页查询数
     * @return 响应信息
     */
    @GetMapping("list")
    public Result GetContestList(Integer pageNumber, Integer listNumber) {

        return Result.success(contestService.GetContestList(pageNumber, listNumber));
    }

    /**
     * 查询比赛信息
     * @param request 获取 token
     * @param contestId 比赛的id
     * @return 返回响应
     */
    @GetMapping("info/{contestId}")
    public Result GetContestInfo(HttpServletRequest request, @PathVariable Integer contestId) {
        Claims claims = (Claims) request.getAttribute("claims");
        Integer userId = (Integer) claims.get("id");
        Object info = contestService.getContestInfo(contestId, userId);
        if(info instanceof String) {
            return Result.error((String) info,401);
        }
        return Result.success(info);
    }

    /**
     * 查询比赛题目
     * @param request 获取 token
     * @param contestId 比赛id
     * @return 返回响应
     */
    @GetMapping("problem")
    public Result GetContestProblem(HttpServletRequest request, Integer contestId) {
        Claims claims = (Claims) request.getAttribute("claims");
        Integer userId = (Integer) claims.get("id");
        return Result.success(contestService.getContestProblemList(contestId, userId));
    }

    /**
     * 创建比赛
     * @param request 获取token
     * @param info ContestInfo
     * @return 返回响应
     */
    @PostMapping("add")
    public Result CreateContest(HttpServletRequest request, @RequestBody ContestInfo info) {
        Claims claims = (Claims) request.getAttribute("claims");
        log.info("用户:{} 进行新增:\"{}\" 比赛操作", claims.get("id"), info.getContestId());
        if(claims.get("permissions").equals("admin")){
            info.setCreator((String) claims.get("nickname"));
            contestService.addContest(info);
            return Result.success("创建成功");

        }
        return Result.error("没有权限",403);
    }

    /**
     * 删除比赛
     * @param request 获取 token
     * @param contestId 比赛ID
     * @return 返回响应
     */
    @DeleteMapping("del")
    public Result DeleteContest(HttpServletRequest request, Integer contestId) {
        Claims claims = (Claims) request.getAttribute("claims");
        log.info("用户:{} 进行删除:{} 比赛操作", claims.get("id"), contestId);
        if(claims.get("permissions").equals("admin")){
            contestService.deleteContest(contestId);
            return Result.success("删除成功");

        }
        return Result.error("没有权限",403);
    }

    /**
     * 修改比赛用户
     * @param request 获取 token
     * @param info ContestUserListInfo
     * @return 返回响应
     */
    @PutMapping("user")
    public Result ChangeContestUser(HttpServletRequest request, @RequestBody ContestUserListInfo info) {
        Claims claims = (Claims) request.getAttribute("claims");
        log.info("用户:{} 进行修改:{} 比赛用户列表操作", claims.get("id"), info.getContestId());
        if(claims.get("permissions").equals("admin")){
            contestService.updateContestUserList(info);
            return Result.success("修改成功");

        }
        return Result.error("没有权限",403);
    }

    /**
     * 修改比赛信息
     * @param request 获取 token
     * @param info ContestInfo
     * @return 返回响应
     */
    @PutMapping("update")
    public Result ChangeContest(HttpServletRequest request, @RequestBody ContestInfo info) {
        Claims claims = (Claims) request.getAttribute("claims");
        log.info("用户:{} 进行修改:{} 比赛操作", claims.get("id"), info.getContestId());
        if(claims.get("permissions").equals("admin")){
            contestService.updateContest(info);
            return Result.success("修改成功");

        }
        return Result.error("没有权限",403);
    }

    /**
     * 查询比赛提交记录
     * @param contestId 比赛Id
     * @param pageNumber 页数
     * @param listNumber 每页查询数
     * @return 返回响应
     */
    @GetMapping("record")
    public Result GetContestRecord(Integer contestId, Integer pageNumber, Integer listNumber) {
        return Result.success(contestService.getContestRecord(contestId, pageNumber, listNumber));
    }

    /**
     * 查询比赛提交信息
     * @param recordId 记录Id
     * @return 返回响应
     */
    @GetMapping("record-info/{recordId}")
    public Result GetContestRecordInfo(HttpServletRequest request, @PathVariable Integer recordId) {
        String jwt = request.getHeader("Accept-token");
        Integer userId = null;
        if(jwt !=null && !jwt.isEmpty()) {
            Claims claims = JwtUtils.parseJWT(jwt);
            if(claims != null){
                userId = (Integer) claims.get("id");
            }
        }
        return Result.success(contestService.getContestRecordInfo(recordId, userId));
    }

    /**
     * 查询比赛用户列表
     * @param request 获取 token
     * @param contestId 比赛的id
     * @return 返回响应
     */
    @GetMapping("user/{contestId}")
    public Result GetContestUserList(HttpServletRequest request, @PathVariable Integer contestId) {
        Claims claims = (Claims) request.getAttribute("claims");
        if(claims.get("permissions").equals("admin")){
            return Result.success(contestService.getContestUserList(contestId));
        }
        return Result.error("没有权限",403);
    }
}
